package org.idontknow.blocks;

import org.apache.log4j.Logger;

class Stack {

	public static Logger log = Logger.getLogger(Stack.class.getName());

	private Block[] stack;

	int currentNumberOfBlocks;

	Stack(int size) {
		log.debug("Constructing a stack of size " + size);
		stack = new Block[size];
		currentNumberOfBlocks = 0;
	}

	public void put(Block block) {
		if (currentNumberOfBlocks >= stack.length)
			log.fatal("App fatal error: impossible to put to a full stack!");
		else
			stack[currentNumberOfBlocks++] = block;
	}

	public Block pop() {
		if (currentNumberOfBlocks == 0) {
			log.fatal("App fatal error: this stack is already empty, can't pop anymore!");
			return null;
		} else
			return stack[currentNumberOfBlocks-- - 1];
	}

	public boolean isClean(int pos) {
		return (getNbBlocksUpon(pos) == 0);
	}

	public int getNbBlocks() {
		return currentNumberOfBlocks;
	}

	public int getNbBlocksUpon(int pos) {
		return currentNumberOfBlocks - pos - 1;
	}

	public Block getBlock(int pos) {
		return stack[pos];
	}

	public void print() {
		for (int i = 0; i < currentNumberOfBlocks; i++)
			System.out.print(stack[i].getValue() + "  ");
	}
}
